package com.quanshu.exchange.ui.gui.profile;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.ShopInterface;
import com.quanshu.exchange.support.entity.CattleIntegralEntity;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.DateUtil;
import com.quanshu.exchange.support.utils.LogUtil;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.recycler.BaseViewHolder;
import com.quanshu.exchange.ui.base.recycler.DataDisplayer;
import com.quanshu.exchange.ui.base.recycler.LayoutWrapper;
import com.quanshu.exchange.ui.base.recycler.SingleAdapter;
import com.quanshu.shop.base.view.BaseTitleBar;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 牛积分 交易记录
 * Created by xiniu003 on 2018/1/21.
 */

public class CattleIntegralActivity extends BaseActivity {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.cattle_title)
    BaseTitleBar cattleTitle;

    private List<CattleIntegralEntity> datas;//全部数据
    private SingleAdapter<CattleIntegralEntity> adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cattle_integral);
        ButterKnife.bind(this);

        initView();
        getTransRecord();
        initListener();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    private void initListener() {
        cattleTitle.setLeftImage(R.drawable.icon_back_black_2_0).setCenterText("牛积分交易记录").setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {

            }
        });
    }

    private void initView() {
        ImmersionBar.with(this).titleBar(cattleTitle).statusBarColor(R.color.white).statusBarDarkFont(true).init();
        recyclerView.setLayoutManager(new LinearLayoutManager(CattleIntegralActivity.this));
        adapter = new SingleAdapter<>(new DataDisplayer<CattleIntegralEntity>(this,
                R.layout.item_cattle_integral) {
            @Override
            public void bindData(BaseViewHolder holder,
                                 LayoutWrapper<CattleIntegralEntity> wrapper,
                                 CattleIntegralEntity item,
                                 int position) {

                holder.setText(R.id.deal_type, item.getType());

                TextView textView = holder.getView(R.id.deal_nums);
                holder.setText(R.id.deal_nums, item.getDetail());

                double amt = Double.parseDouble(item.getIncrease());

                if (amt > 0) {

                    textView.setTextColor(getResources().getColor(R.color.text_socre_amt_big));

                } else {

                    textView.setTextColor(getResources().getColor(R.color.text_socre_amt_little));
                }


                holder.setText(R.id.deal_time,
                        DateUtil.TimeStamp2Date(item.getTrans_time(), DateUtil.FORMAT_YMDHMS));

                holder.setText(R.id.residue_nums, "来源：" + item.getUser_id());

            }
        });
        recyclerView.setAdapter(adapter);
    }


    private void getTransRecord() {

        NetClient.getInstance()
                .getShopService(ShopInterface.class)
                .getTransRecord(AppUtils.getSignParams(String.valueOf(currentUser.getId())),
                        String.valueOf(currentUser.getId()),"1")
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse<List<CattleIntegralEntity>>>() {
                    @Override
                    public void onSuccess(BaseResponse<List<CattleIntegralEntity>> response) {
                        if (response.getData() != null && response.getData().size() > 0) {
                            datas = response.getInfo();
                            adapter.setData(datas);
                        } else {
                            ToastHelper.toast("暂无牛积分交易记录");
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        LogUtil.printException(e);
                    }
                });
    }


}
